package com.hubu.game.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hubu.game.entity.pojo.User;
import com.hubu.game.service.IUserService;
import com.hubu.game.utils.Result;
import com.hubu.game.utils.ResultCode;
import com.hubu.game.entity.vo.LoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;

@RestController
@RequestMapping("/game/user")
@Api("登录接口")
public class LoginController {

    @Resource
    private IUserService userService;

    @PostMapping("login")
    @ApiOperation("登录")
    public Result login(@RequestBody LoginVo loginVo){
        //获取用户名和密码
        String username = loginVo.getUsername();
        String password = loginVo.getPassword();
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,username).eq(User::getPassword,password);

        User searchedUser = userService.getOne(queryWrapper);
        //判断是否存在此用户
        if(searchedUser==null){
            return Result.fail(ResultCode.USER_ACCOUNT_NOT_EXIST);
        }

        //存在此用户，则返回token
        HashMap<String, String> map = new HashMap<>();
        map.put("accessToken","m-token");

        return Result.success(map);
    }
}
